<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('ADMIN_PAGES')) { exit; }
if (!can_admin('settings')) { die('Access Denied'); }

abstract class Dragonfly_Admin_Settings
{
	protected static
		$sections = array(_SYSTEM, _MAINTENANCE, _BROWSER_COOKIES, _FOOTER, _BACKENDCONF, _COMMENTSOPT, _CENSOROPTIONS, _EMAILOPTIONS, _DEBUG, _MISCOPT);

	public static function GET()
	{
		$section = isset($_GET['s']) ? intval($_GET['s']) : 0;
		if (is_writeable(CORE_PATH.'config.php')) { self::$sections[11] = 'config.php'; }

		# check for valid section
		if (!isset(self::$sections[$section])) { URL::redirect(URL::admin()); }

		global $MAIN_CFG, $cpgtpl;

		$cpgtpl->assign_vars(array(
			'B_ADMIN_HELP' => $MAIN_CFG->global->admin_help,
			'S_CPG_NUKE' => CPG_NUKE,
			'S_SECTION' => $section,
			'S_ACTION' => URL::admin('&amp;s='.$section)
		));

		if ($section == 0) {
			$timezones['UTC'] = 'UTC';
			foreach (timezone_identifiers_list() as $tz) { $timezones[$tz] = $tz; }

			//if (strpos($_SERVER['SERVER_SOFTWARE'], 'IIS')) $MAIN_CFG->seo->leo = 0;
			$avail_settings = array(
				array(
					'L_TITLE' => _SITENAME,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[sitename]',
					'S_VALUE' => htmlprepare($MAIN_CFG->global->sitename),
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => _SITE_DOMAIN,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'server[domain]',
					'S_VALUE' => $MAIN_CFG->server->domain,
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 50
				),
				array(
					'L_TITLE' => _SITE_PATH,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'server[path]',
					'S_VALUE' => $MAIN_CFG->server->path,
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 100
				),
				array(
					'L_TITLE' => _SITE_TIMEZONE,
					'L_TOOLTIP' => show_tooltip('site_timezone'),
					'B_INPUT' => false,
					'S_TYPE' => select_option('global[timezone]', $MAIN_CFG->global->timezone, $timezones)
				),
				array(
					'L_TITLE' => _SITELOGO,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[site_logo]',
					'S_VALUE' => $MAIN_CFG->global->site_logo,
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => _SITESLOGAN,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[slogan]',
					'S_VALUE' => htmlprepare($MAIN_CFG->global->slogan),
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => _STARTDATE,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[startdate]',
					'S_VALUE' => htmlprepare($MAIN_CFG->global->startdate),
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 50
				),
				array(
					'L_TITLE' => _ADMINEMAIL,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[adminmail]',
					'S_VALUE' => $MAIN_CFG->global->adminmail,
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => _BREADCRUMB,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[crumb]',
					'S_VALUE' => isset($MAIN_CFG->global->crumb) ? htmlprepare($MAIN_CFG->global->crumb) : _BC_DELIM,
					'S_SIZE' => 8,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => _TOOLTIPS,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[admin_help]', $MAIN_CFG->global->admin_help)
				),
				array(
					'L_TITLE' => _UM_TOGGLE,
					'L_TOOLTIP' => show_tooltip('update_monitor'),
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[update_monitor]', $MAIN_CFG->global->update_monitor)
				),
				array(
					'L_TITLE' => _BLOCK_FRAMES,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[block_frames]', $MAIN_CFG->global->block_frames)
				),
				array(
					'L_TITLE' => _DEFAULTTHEME,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_option('global[Default_Theme]', $MAIN_CFG->global->Default_Theme, cpg_template::getThemes())
				),
				array(
					'L_TITLE' => _ACTIVATE_LEO,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('seo[leo]', $MAIN_CFG->seo->leo)
				),
				array(
					'L_TITLE' => 'LEO url ending',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_option('seo[leoend]', $MAIN_CFG->seo->leoend, array('.html', '/', ''))
				),
				array(
					'L_TITLE' => 'LEO url case',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_option('seo[leocase]', $MAIN_CFG->seo->leocase, array('Regular', 'lower'))
				),
				//array(
				//	'L_TITLE' => 'Use canonical tag in header',
				//	'L_TOOLTIP' => '',
				//	'B_INPUT' => false,
				//	'S_TYPE' => yesno_option('seo[canonical]', $MAIN_CFG->seo->canonical)
				//)
			);
		}

		elseif ($section == 1) {
			$avail_settings = array(
				array(
					'L_TITLE' => _ACTIVE,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[maintenance]', $MAIN_CFG->global->maintenance)
				),
				array(
					'L_TITLE' => _MESSAGE,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<textarea name="global[maintenance_text]" cols="42" rows="5">'.htmlprepare($MAIN_CFG->global->maintenance_text).'</textarea>'
				)
			);
		}

		elseif ($section == 2) {
			$avail_settings = array(
				array(
					'L_TITLE' => _SNAME_AS_COOKIE,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('cookie[server]', intval($MAIN_CFG->cookie->server)).' '._CURRENT.': '.str_replace('www.', '', $_SERVER['SERVER_NAME'])
				),
				array(
					'L_TITLE' => _COOKIE_DOMAIN,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'cookie[domain]',
					'S_VALUE' => $MAIN_CFG->cookie->domain,
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => _COOKIE_PATH,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'cookie[path]',
					'S_VALUE' => $MAIN_CFG->cookie->path,
					'S_SIZE' => 45,
					'S_MAXLENGTH' => 100
				),
			);
		}

		elseif ($section == 3) {
			$avail_settings = array(
				array(
					'L_TITLE' => _FOOTERMSG,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<textarea name="global[foot1]" cols="50" rows="7">'.htmlprepare($MAIN_CFG->global->foot1).'</textarea>'
				),
				array(
					'L_TITLE' => _FOOTERLINE2,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<textarea name="global[foot2]" cols="50" rows="7">'.htmlprepare($MAIN_CFG->global->foot2).'</textarea>'
				),
				array(
					'L_TITLE' => _FOOTERLINE3,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<textarea name="global[foot3]" cols="50" rows="7">'.htmlprepare($MAIN_CFG->global->foot3).'</textarea>'
				)
			);
		}

		elseif ($section == 4) {
			$avail_settings = array(
				array(
					'L_TITLE' => _BACKENDTITLE,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[backend_title]',
					'S_VALUE' => htmlprepare($MAIN_CFG->global->backend_title),
					'S_SIZE' => 40,
					'S_MAXLENGTH' => 100
				),
				array(
					'L_TITLE' => _BACKENDLANG,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[backend_language]',
					'S_VALUE' => $MAIN_CFG->global->backend_language,
					'S_SIZE' => 10,
					'S_MAXLENGTH' => 10
				)
			);
		}

		elseif ($section == 5) {
			$avail_settings = array(
				array(
					'L_TITLE' => _COMMENTSLIMIT,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input type="number" name="global[commentlimit]" value="'.$MAIN_CFG->global->commentlimit.'" min="128" size="4"/>'
				),
				array(
					'L_TITLE' => _COMMENTSPOLLS,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[pollcomm]', $MAIN_CFG->global->pollcomm)
				),
				array(
					'L_TITLE' => _COMMENTSMOD,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_box('global[moderate]', $MAIN_CFG->global->moderate, array(_NOMOD,_MODADMIN,_MODUSERS))
				)
			);
		}

		elseif ($section == 6) {
			$avail_settings = array(
				array(
					'L_TITLE' => _CENSORMODE,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_box('global[CensorMode]', $MAIN_CFG->global->CensorMode, array(_NOFILTERING,_EXACTMATCH,_MATCHBEG,_MATCHANY))
				),
				array(
					'L_TITLE' => _CENSORREPLACE,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'global[CensorReplace]',
					'S_VALUE' => $MAIN_CFG->global->CensorReplace,
					'S_SIZE' => 10,
					'S_MAXLENGTH' => 10
				)
			);
			$CensorList = explode('|',$MAIN_CFG->global->CensorList);
			foreach (explode('|',$MAIN_CFG->global->CensorList) as $word) {
				$cpgtpl->assign_block_vars('censorlist', array('S_VALUE'=>$word));
			}
		}

		elseif ($section == 7) {
			$avail_settings = array(
				array(
					'L_TITLE' => _ALLOW_HTML_EMAIL,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('email[allow_html_email]', $MAIN_CFG->email->allow_html_email)
				),
				array(
					'L_TITLE' => _USE_SMTP,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('email[smtp_on]', $MAIN_CFG->email->smtp_on)
				),
				array(
					'L_TITLE' => _SMTP_HOST,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'email[smtphost]',
					'S_VALUE' => $MAIN_CFG->email->smtphost,
					'S_SIZE' => 25,
					'S_MAXLENGTH' => 100
				),
				array(
					'L_TITLE' => _USE_SMTP_AUTH,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('email[smtp_auth]', $MAIN_CFG->email->smtp_auth)
				),
				array(
					'L_TITLE' => _SMTP_USER_NAME,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'email[smtp_uname]',
					'S_VALUE' => $MAIN_CFG->email->smtp_uname,
					'S_SIZE' => 25,
					'S_MAXLENGTH' => 50
				),
				array(
					'L_TITLE' => _SMTP_USER_PASS,
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'email[smtp_pass]',
					'S_VALUE' => $MAIN_CFG->email->smtp_pass,
					'S_SIZE' => 25,
					'S_MAXLENGTH' => 50
				),
			);
		}

		elseif ($section == 8) {
			$error_level = $MAIN_CFG->debug->error_level;
			$log_level = $MAIN_CFG->debug->log_level;
			$avail_settings = array(
				array(
					'L_TITLE' => 'PHP Notices',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="php_nd" type="checkbox" name="error_level[]" value="'.E_NOTICE.'" '.(($error_level & E_NOTICE) ? 'checked="checked"' : '').' /><label for="php_nd">Display</label><br />
					<input id="php_nl" type="checkbox" name="log_level[]" value="'.E_NOTICE.'" '.(($log_level & E_NOTICE) ? 'checked="checked"' : '').' /><label for="php_nl">Log</label>',
				),
				array(
					'L_TITLE' => 'PHP Warnings',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="php_wd" type="checkbox" name="error_level[]" value="'.E_WARNING.'" '.(($error_level & E_WARNING) ? 'checked="checked"' : '').' /><label for="php_wd">Display</label><br />
					<input id="php_wl" type="checkbox" name="log_level[]" value="'.E_WARNING.'" '.(($log_level & E_WARNING) ? 'checked="checked"' : '').' /><label for="php_wl">Log</label>',
				),
				array(
					'L_TITLE' => 'PHP Strict',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="php_sd" type="checkbox" name="error_level[]" value="'.E_STRICT.'" '.(($error_level & E_STRICT) ? 'checked="checked"' : '').' /><label for="php_sd">Display</label><br />
					<input id="php_sl" type="checkbox" name="log_level[]" value="'.E_STRICT.'" '.(($log_level & E_STRICT) ? 'checked="checked"' : '').' /><label for="php_sl">Log</label>',
				),
				array(
					'L_TITLE' => 'PHP Recoverable Error',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="php_rd" type="checkbox" name="error_level[]" value="'.E_RECOVERABLE_ERROR.'" '.(($error_level & E_RECOVERABLE_ERROR) ? 'checked="checked"' : '').' /><label for="php_rd">Display</label><br />
					<input id="php_rl" type="checkbox" name="log_level[]" value="'.E_RECOVERABLE_ERROR.'" '.(($log_level & E_RECOVERABLE_ERROR) ? 'checked="checked"' : '').' /><label for="php_rl">Log</label>',
				),
				array(
					'L_TITLE' => 'PHP Deprecated',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="php_dd" type="checkbox" name="error_level[]" value="'.E_DEPRECATED.'" '.(($error_level & E_DEPRECATED) ? 'checked="checked"' : '').' /><label for="php_dd">Display</label><br />
					<input id="php_dl" type="checkbox" name="log_level[]" value="'.E_DEPRECATED.'" '.(($log_level & E_DEPRECATED) ? 'checked="checked"' : '').' /><label for="php_dl">Log</label>',
				),
				array(
					'L_TITLE' => 'CMS Notices',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="cms_nd" type="checkbox" name="error_level[]" value="'.E_USER_NOTICE.'" '.(($error_level & E_USER_NOTICE) ? 'checked="checked"' : '').' /><label for="cms_nd">Display</label><br />
					<input id="cms_nl" type="checkbox" name="log_level[]" value="'.E_USER_NOTICE.'" '.(($log_level & E_USER_NOTICE) ? 'checked="checked"' : '').' /><label for="cms_nl">Log</label>',
				),
				array(
					'L_TITLE' => 'CMS Warnings',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="cms_wd" type="checkbox" name="error_level[]" value="'.E_USER_WARNING.'" '.(($error_level & E_USER_WARNING) ? 'checked="checked"' : '').' /><label for="cms_wd">Display</label><br />
					<input id="cms_wl" type="checkbox" name="log_level[]" value="'.E_USER_WARNING.'" '.(($log_level & E_USER_WARNING) ? 'checked="checked"' : '').' /><label for="cms_wl">Log</label>',
				),
				array(
					'L_TITLE' => 'CMS Deprecated',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="cms_dd" type="checkbox" name="error_level[]" value="'.E_USER_DEPRECATED.'" '.(($error_level & E_USER_DEPRECATED) ? 'checked="checked"' : '').' /><label for="cms_dd">Display</label><br />
					<input id="cms_dl" type="checkbox" name="log_level[]" value="'.E_USER_DEPRECATED.'" '.(($log_level & E_USER_DEPRECATED) ? 'checked="checked"' : '').' /><label for="cms_dl">Log</label>',
				),
				array(
					'L_TITLE' => 'CMS Errors',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input id="cms_dd" type="checkbox" name="error_level[]" value="'.E_USER_ERROR.'" '.(($error_level & E_USER_ERROR) ? 'checked="checked"' : '').' /><label for="cms_dd">Display</label><br />
					<input id="cms_dl" type="checkbox" name="log_level[]" value="'.E_USER_ERROR.'" '.(($log_level & E_USER_ERROR) ? 'checked="checked"' : '').' /><label for="cms_dl">Log</label>',
				),
				array(
					'L_TITLE' => 'Database Queries Display',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('debug[database]', $MAIN_CFG->debug->database)
				),
				array(
					'L_TITLE' => "Show session data on error pages",
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('debug[session]', $MAIN_CFG->debug->session)
				)
			);
		}

		elseif ($section == 9) {
			Dragonfly_Output_Js::inline('
			function adminGraphSettings(e) {
				var el, i=-1, elements=Poodle.$("settings").$T("input");
				while (el=elements[++i]) {
					if ("admingraphic[]" == el.name) {
						if (2 == el.value) continue;
						if (!e) {
							el.bind("click", adminGraphSettings);
						} else if ("click" == e.type) {
							el.initValue(e.target.value == el.value);
						}
					}
				}
			}
			Poodle.onDOMReady(function(event){adminGraphSettings(event);});
			');
			$img_options = array(''=>'[Auto select]');
			foreach (\Poodle\Image::getHandlers() as $name => $info) {
				$img_options[$name] = $info['name'];
			}

			$avail_settings = array(
				array(
					'L_TITLE' => _ACTBANNERS,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[banners]', $MAIN_CFG->global->banners)
				),
				array(
					'L_TITLE' => _ACTIVATEHTTPREF,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('global[httpref]', $MAIN_CFG->global->httpref)
				),
				array(
					'L_TITLE' => _MAXREF,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input type="number" name="global[httprefmax]" value="'.$MAIN_CFG->global->httprefmax.'" min="0" size="4"/>'
				),
				array(
					'L_TITLE' => _ITEMSTOP,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => '<input type="number" name="global[top]" value="'.$MAIN_CFG->global->top.'" min="5" max="30" size="4"/>'
				),
				array(
					'L_TITLE' => _GRAPHICOPT,
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' =>	'
						<input type="checkbox" name="admingraphic[]" value="1"
							'.(($MAIN_CFG->global->admingraphic & Dragonfly_Admin_Menu::GRAPH) ? 'checked="checked"' : '').'/>
							'._GRAPHICAL.'<br />
						<input type="checkbox" name="admingraphic[]" value="16"
							'.(($MAIN_CFG->global->admingraphic & Dragonfly_Admin_Menu::TABS) ? 'checked="checked"' : '').'/>
							'._GRAPHICAL.' tabbed<br />
						<input type="checkbox" name="admingraphic[]" value="4"
							'.(($MAIN_CFG->global->admingraphic & Dragonfly_Admin_Menu::CSS) ? 'checked="checked"' : '').'/>
							CSS<br />
						<input type="checkbox" name="admingraphic[]" value="8"
							'.(($MAIN_CFG->global->admingraphic & Dragonfly_Admin_Menu::JS) ? 'checked="checked"' : '').'/>
							DHTML<br />
						<input type="checkbox" name="admingraphic[]" value="2"
							'.(($MAIN_CFG->global->admingraphic & Dragonfly_Admin_Menu::BLOCK) ? 'checked="checked"' : '').'/>
							'._SIDEBLOCK.'<br />
							'
				),
				array(
					'L_TITLE' => 'Image Processing and Generation',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_box('image[handler]', $MAIN_CFG->image->handler, $img_options)
				)
			);
		}

		elseif ($section == 11) {
			global $adminindex, $mainindex;
			$ignore = array('banners.php', 'error.php', 'header.php', 'footer.php', 'install.php');
			$filesa = $filesi = array();
			$dir = dir(BASEDIR);
			while ($file = $dir->read()) {
				if (is_file(BASEDIR.$file) && preg_match('#\.php$#', $file) && !in_array($file, $ignore)) {
					$filesi[] = $file;
				}
			}
			$dir->close();
			natcasesort($filesa);
			natcasesort($filesi);
			array_unshift($filesi, '[none]');

			$avail_settings = array(
				array(
					'L_TITLE' => 'Debugging mode',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('config_debug', CPG_DEBUG)
				),
				array(
					'L_TITLE' => 'Activate installer mode',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('config_mode_install', DF_MODE_INSTALL)
				),
				array(
					'L_TITLE' => 'Require HTTP SSL',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => yesno_option('config_ssl_required', DF_HTTP_SSL_REQUIRED)
				),
				array(
					'L_TITLE' => 'Static images domain',
					'L_TOOLTIP' => '',
					'B_INPUT' => true,
					'S_TYPE' => 'text',
					'S_NAME' => 'config_static_domain',
					'S_VALUE' => DF_STATIC_DOMAIN,
					'S_SIZE' => 25,
					'S_MAXLENGTH' => 255
				),
				array(
					'L_TITLE' => 'Index file</span><br /><i>If you change this into something else then index.php or [none]<br/>then you must modify LEO in .htaccess</i>',
					'L_TOOLTIP' => '',
					'B_INPUT' => false,
					'S_TYPE' => select_option('config_index', $mainindex, $filesi)
				)
			);
		}

		$sc = count(self::$sections);
		foreach (self::$sections as $key => $value) {
			$cpgtpl->assign_block_vars('menu', array(
				'B_URL' => $section != $key,
				'S_URL' => URL::admin('&amp;s='.$key),
				'S_VALUE' => $value,
				'S_KEY' => $key,
				'B_NEXT' => --$sc > 0
			));
			if ($key == $section) {
				foreach ($avail_settings as $settings) {
					$cpgtpl->assign_block_vars('settings', $settings);
				}
			}
		}

		$GLOBALS['pagetitle'] .= ' '._BC_DELIM.' '._SITECONFIG.' '._BC_DELIM.' '.self::$sections[$section];
		require('header.php');

		$cpgtpl->set_handle('body', 'admin/settings.html');
		$cpgtpl->display('body');
	}

	public static function POST()
	{
		$section = isset($_GET['s']) ? intval($_GET['s']) : 0;
		if (extension_loaded('gd')) { self::$sections[] = _SECURITYCODE; }
		if (is_writeable(CORE_PATH.'config.php')) { self::$sections[11] = 'config.php'; }

		# check for valid section
		if (!isset(self::$sections[$section])) { URL::redirect(URL::admin()); }

		if (isset($_POST['save']))
		{
			global $MAIN_CFG, $CPG_SESS;

			if ($_POST['save'] != $CPG_SESS['admin']['page']) { cpg_error(_ERROR_BAD_LINK, _SEC_ERROR); }

			if ($section == 11) {
				$fp = fopen(CORE_PATH.'config.php', 'rb');
				$config = fread($fp, filesize(CORE_PATH.'config.php'));
				fclose($fp);
				$config = preg_replace('#define\(\'DF_MODE_INSTALL\', [a-z]+\)#s', 'define(\'DF_MODE_INSTALL\', '.($_POST['config_mode_install'] ? 'true' : 'false').')', $config);
				$config = preg_replace('#define\(\'DF_STATIC_DOMAIN\', \'.*?\'\)#s', 'define(\'DF_STATIC_DOMAIN\', \''.Fix_Quotes($_POST['config_static_domain']).'\')', $config);
				$config = preg_replace('#define\(\'DF_HTTP_SSL_REQUIRED\', [a-z]+\)#s', 'define(\'DF_HTTP_SSL_REQUIRED\', '.($_POST['config_ssl_required'] ? 'true' : 'false').')', $config);
				$config = preg_replace('#\$mainindex[\s]+=[\s]+\'.*?\'#s', '$mainindex = \''.(($_POST['config_index']=='[none]')?'':$_POST['config_index']).'\'', $config);
				$config = preg_replace('#define\(\'CPG_DEBUG\', [a-z]+\)#s', 'define(\'CPG_DEBUG\', '.($_POST['config_debug'] ? 'true' : 'false').')', $config);
				$config = preg_replace('#\$sitekey = \'.*?\';#s', '', $config);
				$written = false;
				if ($fp = fopen(CORE_PATH.'config.php', 'wb')) {
					$written = fwrite($fp, $config);
					fclose($fp);
				}
				if (!$written) { cpg_error('Failed modifying file.'); }
				URL::redirect(URL::admin('&s='.$section));
			}

			$sections = array(
				array('global' => array('sitename', 'timezone', 'dateformat', 'site_logo', 'slogan', 'startdate', 'adminmail', 'crumb', 'admin_help', 'update_monitor', 'GoogleTap', 'block_frames', 'Default_Theme'),
					  'server' => array('timezone', 'domain', 'path'),
						 'seo' => array('leo', 'leoend', 'leocase')),
				array('global' => array('maintenance', 'maintenance_text')),
				array('cookie' => array('server', 'domain', 'path')),
				array('global' => array('foot1', 'foot2', 'foot3')),
				array('global' => array('backend_title', 'backend_language')),
				array('global' => array('commentlimit', 'pollcomm', 'moderate')),
				array('global' => array('CensorMode', 'CensorReplace')),
				array('email'  => array('allow_html_email', 'smtp_on', 'smtphost', 'smtp_auth', 'smtp_uname', 'smtp_pass')),
				array('debug'  => array('database', 'session', 'error_level', 'log_level')),
				array('global' => array('banners', 'httpref', 'httprefmax', 'top'),
				      'image'  => array('handler'))
			);

			if (isset($sections[$section])) {
				foreach ($sections[$section] as $area => $keys) {
				foreach ($keys as $key) {
					if (isset($_POST[$area][$key])) {
						$value = trim($_POST[$area][$key]);
						if ($key == 'path') {
							if (substr($value, -1) != '/') $value .= '/';
							if ($value[0] != '/') $value = '/'.$value;
						}
						elseif ($key == 'Default_Theme') { $CPG_SESS['theme'] = $value; }
						$MAIN_CFG->set($area, $key, $value);
					}
				}
				}
			}

			if ($section == 6) {
				$list = array();
				foreach ($_POST['global']['CensorList'] as $word) {
					$word = mb_strtolower(trim($word));
					if ($word) { $list[$word] = $word; }
				}
				natcasesort($list);
				$MAIN_CFG->set('global', 'CensorList', implode('|',$list));
			}

			else if ($section == 8) {
				$error_level = $log_level = 0;
				if (isset($_POST['error_level'])) {
					foreach ($_POST['error_level'] as $val) { $error_level |= intval($val); }
				}
				$MAIN_CFG->set('debug', 'error_level', $error_level);
				if (isset($_POST['log_level'])) {
					foreach ($_POST['log_level'] as $val) { $log_level |= intval($val); }
				}
				$MAIN_CFG->set('debug', 'log_level', $log_level);
			}

			elseif ($section == 9) {
				$admingraphic = 0;
				if (isset($_POST['admingraphic'])) {
					foreach ($_POST['admingraphic'] as $val) { $admingraphic |= intval($val); }
				}
				global $CLASS;
				if ($admingraphic == 0) { $admingraphic = Dragonfly_Admin_Menu::BLOCK; }
				$MAIN_CFG->set('global', 'admingraphic', $admingraphic);
			}
		}
		URL::redirect(URL::admin('&s='.$section));
	}
}

Dragonfly_Admin_Settings::$_SERVER['REQUEST_METHOD']();
